#include "StdAfx.h"
#include "include/FolderViewer.h"
#include "QModelViewer.h"

FolderViewer::FolderViewer(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_pFileModel = new QFileSystemModel(this);
	m_pFileModel->setRootPath(getProgramPath());
	m_pFileModel->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
	QModelIndex index = m_pFileModel->index("F:\\develop\\OgreSDK_1.7.2\\bin\\SuperMon_d");	
	ui.m_pFolderView->setModel(m_pFileModel);
	ui.m_pFolderView->setColumnHidden(1, true);
	ui.m_pFolderView->setColumnHidden(2, true);
	ui.m_pFolderView->setColumnHidden(3, true);
	ui.m_pFolderView->collapse(index);
	connect(ui.m_pFolderView, SIGNAL(clicked(const QModelIndex&)),
			this, SLOT(onFolderViewClicked(const QModelIndex&)));
	connect(ui.m_pFileListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
			this, SLOT(onFileIitemDoubleClicked(QListWidgetItem*)));
	//ui.m_pFileView->setModel(m_pFileModel);
	//ui.m_pFileView->setColumnHidden(1, true);
	//ui.m_pFileView->setColumnHidden(2, true);
	//ui.m_pFileView->setColumnHidden(3, true);
}

FolderViewer::~FolderViewer()
{

}

void FolderViewer::onFolderViewClicked(const QModelIndex &index)
{
	m_sCurrentPath = m_pFileModel->filePath(index) + "/";
	ui.m_pPathEdit->setText(m_sCurrentPath);
	QDir dir(m_sCurrentPath, "*.mesh", QDir::IgnoreCase, QDir::Files);
	ui.m_pFileListWidget->clear();
	QStringList fileList = dir.entryList(QDir::Files);
	QStringList::iterator itr = fileList.begin();
	QStringList::iterator end = fileList.end();
	while(itr != end)
	{
		ui.m_pFileListWidget->addItem(*itr);
		++itr;
	}
}

void FolderViewer::onFileIitemDoubleClicked(QListWidgetItem* item)
{
	//item->text();
	QDir dir(getProgramPath());
	QString relativePath = dir.relativeFilePath(m_sCurrentPath);
	Ogre::String resLocation = relativePath.toLocal8Bit().data();
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation(resLocation,
		"FileSystem", resLocation);
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup(resLocation);
	m_pModelViewer->loadEntity(relativePath + "/" + item->text());
}

void FolderViewer::setQModelViewer(QModelViewer* pModelViewer)
{
	m_pModelViewer = pModelViewer;
}